package com.cwl.pulgins.utils;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.StringWriter;
import java.util.Properties;

/**
 * @program: pulgin
 * @description:
 * @author: ChenWenLong
 * @create: 2019-09-03 15:57
 **/
public class VelocityUtils {

    public static String render(String templateFile, VelocityContext velocityContext) {
        Properties p = new Properties();
        p.put("file.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(p);
        Template template = Velocity.getTemplate(templateFile);
        StringWriter stringWriter = new StringWriter();
        template.merge(velocityContext, stringWriter);
        template.process();
        return stringWriter.toString();
    }

    public static void main(String[] args) {
        Properties p = new Properties();
        p.put("file.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(p);
        Template template = Velocity.getTemplate("create_table.vm");
        StringWriter stringWriter = new StringWriter();
        VelocityContext velocityContext = new VelocityContext();
        template.merge(velocityContext, stringWriter);
        template.process();
    }
}
